package cn.com.zhbs.login.service.impl;

import cn.com.zhbs.account.dao.AccountsMapper;
import cn.com.zhbs.common.jedis.JedisClient;
import cn.com.zhbs.common.util.Encrypt;
import cn.com.zhbs.common.Result.SimpleFactory.ResultFactory;
import cn.com.zhbs.login.dao.LoginMapper;
import cn.com.zhbs.account.entitys.Account;
import cn.com.zhbs.login.service.LoginService;
import cn.com.zhbs.login.util.Util;
import cn.com.zhbs.netty.utils.SysConfig;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.logging.Logger;

/**
 * @author 陈治杰
 * @Title: LoginServiceImpl
 * @Description:
 * @date 2019/2/1315:05
 */
@Service("loginService")
public class LoginServiceImpl implements LoginService {
    private Logger logger=Logger.getLogger(this.getClass().getName());
    @Resource
    LoginMapper loginDao;

    @Resource
    JedisClient jedisClient;

    @Autowired
    private AccountsMapper accountsMapper;

    @Override
    public Map<String,Object> login_account(JSONObject jsonObject, HttpServletRequest request) throws Exception{
        Map<String,Object> resultMap=new HashMap<>();
        HttpSession session=request.getSession();//创建session对象
        jsonObject.put(Account.password.toString(), Encrypt.encrypt(jsonObject.getString(Account.account.toString())+jsonObject.getString(Account.password.toString())));
        Map<String,Object> accountMap=loginDao.account(jsonObject);
        if(accountMap==null){
            jsonObject.put(Account.mobilephone.toString(),jsonObject.getString(Account.account.toString()));
            String account=accountsMapper.isPhoneExist(jsonObject);
            if(account==null){
                return ResultFactory.failure("0","账号密码错误",null).display();
            }else{
                accountMap=loginDao.account(jsonObject);
                if(accountMap==null){
                    return ResultFactory.failure("0","账号密码错误",null).display();
                }
            }
        }
        Integer superRole=loginDao.isSuperRole(accountMap);
        accountMap.put("superRole",superRole);
        Util.saveMapToSession(session,accountMap);
        //封装返回内容
        JSONObject jsonObject1=new JSONObject(accountMap);
        resultMap.put("depart",accountsMapper.findDNameByAID(jsonObject1));
        resultMap.put(Account.aid.toString(),accountMap.get(Account.aid.toString()));
        resultMap.put(Account.account.toString(),accountMap.get(Account.account.toString()));
        resultMap.put(Account.username.toString(),accountMap.get(Account.username.toString()));
        resultMap.put(Account.mobilephone.toString(),accountMap.get(Account.mobilephone.toString()));
        resultMap.put("superRole",superRole);
        resultMap.put("lastTime",accountMap.get("login_date"));

        //更新登录时间
        loginDao.updateLoginDate(accountMap);
        return ResultFactory.success(resultMap).display();
    }
}
